﻿using Liju.Common;
using Liju.Model;
using LiJu.Db;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace LiJu.Record
{
    public partial class TaskCurve : Form
    {
        /// <summary>
        /// 曲线标题字体
        /// </summary>
        private Font titleFont = new Font("Microsoft YaHei", 12, FontStyle.Bold);
        /// <summary>
        /// 曲线坐标轴字体
        /// </summary>
        private Font axisFont = new Font("Microsoft YaHei", 10, FontStyle.Regular);
        /// <summary>
        /// 曲线颜色，第一个元素表示第一条曲线颜色
        /// </summary>
        List<Color> seriesColor = new List<Color>() { Color.Blue, Color.Orange };
        /// <summary>
        /// X轴参数
        /// </summary>
        public static List<double> Xparams = new List<double>();
        /// <summary>
        /// Y轴参数
        /// </summary>
        public static List<double> Yparams = new List<double>();
        /// <summary>
        /// Y2轴参数(辅轴)
        /// </summary>
        public static List<double> Y2params = new List<double>();

        public static List<double> torqueAngleList = new List<double>();
        public static List<double> curveAngleList = new List<double>();
        public static List<double> torqueTimeList = new List<double>();
        public static List<double> curveTimeList = new List<double>();
        public static double curveBaseDeg = 0;
        public static double curveBaseTime = 0;


        //鼠标按下标志
        bool isMouseDown1 = false;
        // 用于记录鼠标上次移动的点，用于判断是左移还是右移
        private int lastXMove1 = 0;
        private int lastYMove1 = 0;

        /// <summary>
        /// X,Y最大，最小值限制
        /// </summary>
        private double xMinNum1 = 0;
        private double xMaxNum1 = 0;
        private double yMinNum1 = 0;
        private double yMaxNum1 = 0;

        /// <summary>
        /// X,Y放大比例
        /// </summary>
        private double xExtendScale1 = 0;
        private double yExtendScale1 = 0;

        /// <summary>
        /// X,Y拖动位移比例
        /// </summary>
        private double xShiftScale1 = 0;
        private double yShiftScale1 = 0;

        /// <summary>
        /// X,Y参数设置标志位
        /// </summary>
        private bool SetAxisLimitFlag1 = false;

        //鼠标按下标志
        bool isMouseDown2 = false;
        // 用于记录鼠标上次移动的点，用于判断是左移还是右移
        private int lastXMove2 = 0;
        private int lastYMove2 = 0;

        /// <summary>
        /// X,Y最大，最小值限制
        /// </summary>
        private double xMinNum2 = 0;
        private double xMaxNum2 = 0;
        private double yMinNum2 = 0;
        private double yMaxNum2 = 0;

        /// <summary>
        /// X,Y放大比例
        /// </summary>
        private double xExtendScale2 = 0;
        private double yExtendScale2 = 0;

        /// <summary>
        /// X,Y拖动位移比例
        /// </summary>
        private double xShiftScale2 = 0;
        private double yShiftScale2 = 0;

        /// <summary>
        /// X,Y参数设置标志位
        /// </summary>
        private bool SetAxisLimitFlag2 = false;

        /// <summary>
        /// 提示框
        /// </summary>
        ToolTip toolTip = new ToolTip();

        /// <summary>
        /// 当前查询曲线任务ID
        /// </summary>
        private long CurrentTaskId = 0;

        //add by ming
        private string _productSN = string.Empty;

        public TaskCurve(long taskId, string productSN)
        {
            InitializeComponent();
            CurrentTaskId = taskId;
            _productSN = productSN;
            InitChart();
            this.Activated += Passage_Activated;
        }

        /// <summary>
        /// 加载页面即加载数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Passage_Activated(object sender, EventArgs e)
        {
            //LoadChartData();
            LoadDDKChartData();
        }

        /// <summary>
        /// 初始化Chart图表
        /// </summary>
        private void InitChart()
        {
            try
            {
                chart1.MouseDown += Chart1_MouseDown;
                chart1.MouseUp += Chart1_MouseUp;
                chart1.MouseMove += Chart1_MouseMove;

                chart1.MouseEnter += Chart1_MouseEnter;
                chart1.MouseLeave += Chart1_MouseLeave;
                chart1.MouseWheel += Chart1_MouseWheel;

                chart1.Click += Chart1_Click;
                chart1.DoubleClick += Chart1_DoubleClick;

                chart1.Series.Clear();
                chart1.ChartAreas.Clear();
                ChartArea chartArea1 = new ChartArea();
                chart1.ChartAreas.Add(chartArea1);

                Title title1 = new Title()
                {
                    Text = "角度+扭矩",
                    Font = titleFont,
                    ForeColor = Color.Green
                };
                //设置标题
                if (chart1.Titles.Count > 0)
                {
                    chart1.Titles[0] = title1;
                }
                else
                {
                    chart1.Titles.Add(title1);
                }

                chart1.ChartAreas[0].AxisY.MajorGrid.Enabled = true;
                chart1.ChartAreas[0].AxisX.MajorGrid.LineDashStyle = ChartDashStyle.Dash; // X轴主网格线样式
                chart1.ChartAreas[0].AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dash;

                //设置不显示chart自带的滚动条
                chart1.ChartAreas[0].AxisX.ScrollBar.Enabled = false;
                chart1.ChartAreas[0].AxisY.ScrollBar.Enabled = false;
                // 注意不要开启X轴游标，默认不开启，如下设置false或者不设置下列参数
                chart1.ChartAreas[0].CursorX.IsUserEnabled = false;
                chart1.ChartAreas[0].CursorX.AutoScroll = false;
                chart1.ChartAreas[0].CursorX.IsUserSelectionEnabled = false;
                //设置
                chart1.ChartAreas[0].AxisY.ArrowStyle = AxisArrowStyle.None;
                chart1.ChartAreas[0].AxisX.ArrowStyle = AxisArrowStyle.None;

                //chart1.Legends[0].Enabled = false;
                chart1.Legends[0].Position = new ElementPosition(-30, -2, 100, 10);
                chart1.Legends[0].Alignment = StringAlignment.Near;
                chart1.Legends[0].Docking = Docking.Top;

                chart2.MouseDown += Chart2_MouseDown;
                chart2.MouseUp += Chart2_MouseUp;
                chart2.MouseMove += Chart2_MouseMove;

                chart2.MouseEnter += Chart2_MouseEnter;
                chart2.MouseLeave += Chart2_MouseLeave;
                chart2.MouseWheel += Chart2_MouseWheel;

                chart2.Click += Chart2_Click;
                chart2.DoubleClick += Chart2_DoubleClick;
                chart2.Series.Clear();
                chart2.ChartAreas.Clear();
                ChartArea chartArea2 = new ChartArea();
                chart2.ChartAreas.Add(chartArea2);

                Title title2 = new Title()
                {
                    Text = "时间+扭矩",
                    Font = titleFont,
                    ForeColor = Color.Green
                };
                //设置标题
                if (chart2.Titles.Count > 0)
                {
                    chart2.Titles[0] = title2;
                }
                else
                {
                    chart2.Titles.Add(title2);
                }

                chart2.ChartAreas[0].AxisY.MajorGrid.Enabled = true;
                chart2.ChartAreas[0].AxisX.MajorGrid.LineDashStyle = ChartDashStyle.Dash; // X轴主网格线样式
                chart2.ChartAreas[0].AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dash;

                //设置不显示chart自带的滚动条
                chart2.ChartAreas[0].AxisX.ScrollBar.Enabled = false;
                chart2.ChartAreas[0].AxisY.ScrollBar.Enabled = false;
                // 注意不要开启X轴游标，默认不开启，如下设置false或者不设置下列参数
                chart2.ChartAreas[0].CursorX.IsUserEnabled = false;
                chart2.ChartAreas[0].CursorX.AutoScroll = false;
                chart2.ChartAreas[0].CursorX.IsUserSelectionEnabled = false;
                //设置
                chart2.ChartAreas[0].AxisY.ArrowStyle = AxisArrowStyle.None;
                chart2.ChartAreas[0].AxisX.ArrowStyle = AxisArrowStyle.None;

                //chart2.Legends[0].Enabled = false;
                chart2.Legends[0].Position = new ElementPosition(-30, -2, 100, 10);
                chart2.Legends[0].Alignment = StringAlignment.Near;
                chart2.Legends[0].Docking = Docking.Top;


            }
            catch (Exception ex)
            {

                MessageBox.Show($"通道，初始化曲线异常->{ex.Message}");
            }

        }

        #region 缩放曲线
        private void Chart1_MouseWheel(object sender, MouseEventArgs e)
        {
            double x_new_size = 0;//X轴的新ScaleView.Size值
            double y_new_size = 0;//Y轴的新ScaleView.Size值
            if (SetAxisLimitFlag1)
            {
                if (e.Delta > 0)//滚轮向上放大
                {
                    if (chart1.ChartAreas[0].AxisX.ScaleView.Size > chart1.ChartAreas[0].AxisX.Minimum)//设置最大放大值
                    {
                        x_new_size = chart1.ChartAreas[0].AxisX.ScaleView.Size - xExtendScale1;//放大
                    }
                    else
                    {
                        //x_new_size = chart1.ChartAreas[0].AxisX.Minimum;
                    }

                    if (chart1.ChartAreas[0].AxisY.ScaleView.Size > chart1.ChartAreas[0].AxisY.Minimum)//设置最大放大值
                    {
                        y_new_size = chart1.ChartAreas[0].AxisY.ScaleView.Size - yExtendScale1;
                    }
                    else
                    {
                        //y_new_size = chart1.ChartAreas[0].AxisY.Minimum;
                    }

                }
                else//滚轮向下缩小
                {
                    if (chart1.ChartAreas[0].AxisX.ScaleView.Size <= (chart1.ChartAreas[0].AxisX.Maximum - chart1.ChartAreas[0].AxisX.Minimum))//不能无限缩小，缩小的极限就是显示全图
                    {
                        x_new_size = chart1.ChartAreas[0].AxisX.ScaleView.Size + xExtendScale1;
                        if (x_new_size >= (chart1.ChartAreas[0].AxisX.Maximum - chart1.ChartAreas[0].AxisX.Minimum))//最大的Size就是整个Y轴
                        {
                            x_new_size = (chart1.ChartAreas[0].AxisX.Maximum - chart1.ChartAreas[0].AxisX.Minimum);
                        }
                    }
                    if (chart1.ChartAreas[0].AxisY.ScaleView.Size <= (chart1.ChartAreas[0].AxisY.Maximum - chart1.ChartAreas[0].AxisY.Minimum))//不能无限缩小，缩小的极限就是显示全图
                    {
                        y_new_size = chart1.ChartAreas[0].AxisY.ScaleView.Size + yExtendScale1;
                        if (y_new_size >= (chart1.ChartAreas[0].AxisY.Maximum - chart1.ChartAreas[0].AxisY.Minimum))//最大的Size就是整个Y轴
                        {
                            y_new_size = (chart1.ChartAreas[0].AxisY.Maximum - chart1.ChartAreas[0].AxisY.Minimum);
                        }
                    }

                }

                if (x_new_size > 0)
                {
                    chart1.ChartAreas[0].AxisX.ScaleView.Size = Math.Round(x_new_size, 2);
                }
                if (y_new_size > 0)
                {
                    chart1.ChartAreas[0].AxisY.ScaleView.Size = Math.Round(y_new_size, 2);

                }

            }
        }

        private void Chart1_MouseLeave(object sender, EventArgs e)
        {
            isMouseDown1 = false;
            chart1.Cursor = Cursors.Default;
            if (chart1.Focused)
            {
                chart1.Parent.Focus();
            }
            toolTip.Hide(this);
        }

        private void Chart1_MouseEnter(object sender, EventArgs e)
        {
            if (!chart1.Focused)
                chart1.Focus();

            toolTip.Show("单击曲线后可拖动,拉伸曲线;双击回到初始位置", chart1, new Point(chart1.Width / 2, chart1.Height / 2), 3000);
        }
        #endregion

        #region 拖动曲线
        private void Chart1_MouseUp(object sender, MouseEventArgs e)
        {
            isMouseDown1 = false;
            chart1.Cursor = Cursors.Default;
        }

        private void Chart1_MouseDown(object sender, MouseEventArgs e)
        {
            isMouseDown1 = true;
            chart1.Cursor = Cursors.SizeAll;
        }

        private void Chart1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMouseDown1)
            {
                double xvalue = chart1.ChartAreas[0].AxisX.ScaleView.Position;
                double yvalue = chart1.ChartAreas[0].AxisY.ScaleView.Position;
                // 可更改（交换）如下加减1或if条件来设置鼠标移动时曲线移动方向
                if (lastXMove1 != 0 && e.X - lastXMove1 > 0)
                    xvalue -= xShiftScale1;  // 每次移动0.05
                else if (lastXMove1 != 0 && e.X - lastXMove1 < 0)
                    xvalue += xShiftScale1;
                chart1.ChartAreas[0].AxisX.ScaleView.Position = Math.Round(xvalue, 4);
                lastXMove1 = e.X;


                if (lastYMove1 != 0 && lastYMove1 - e.Y > 0)
                    yvalue -= yShiftScale1;
                else if (lastYMove1 != 0 && lastYMove1 - e.Y < 0)
                    yvalue += yShiftScale1;
                chart1.ChartAreas[0].AxisY.ScaleView.Position = Math.Round(yvalue, 4);
                lastYMove1 = e.Y;
            }
        }
        #endregion

        private void Chart1_Click(object sender, EventArgs e)
        {
            try
            {
                if (!SetAxisLimitFlag1)
                {
                    SetAxisLimitFlag1 = true;
                    xMinNum1 = chart1.ChartAreas[0].AxisX.Minimum;
                    xMaxNum1 = chart1.ChartAreas[0].AxisX.Maximum;

                    yMinNum1 = chart1.ChartAreas[0].AxisY.Minimum;
                    yMaxNum1 = chart1.ChartAreas[0].AxisY.Maximum;


                    chart1.ChartAreas[0].AxisX.ScaleView.Size = (xMaxNum1 - xMinNum1);
                    chart1.ChartAreas[0].AxisY.ScaleView.Size = (yMaxNum1 - yMinNum1);

                    xExtendScale1 = (xMaxNum1 - xMinNum1) / 10;
                    xShiftScale1 = xExtendScale1 / 20;

                    yExtendScale1 = (yMaxNum1 - yMinNum1) / 10;
                    yShiftScale1 = yExtendScale1 / 20;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"通道，chart_ScrewDriver_Click->{ex.Message}");
            }
        }

        private void Chart1_DoubleClick(object sender, EventArgs e)
        {
            chart1.ChartAreas[0].AxisX.ScaleView.Size = (xMaxNum1 - xMinNum1);
            chart1.ChartAreas[0].AxisY.ScaleView.Size = (yMaxNum1 - yMinNum1);

            chart1.ChartAreas[0].AxisX.ScaleView.Position = xMinNum1;
            chart1.ChartAreas[0].AxisY.ScaleView.Position = yMinNum1;

        }


        #region 缩放曲线
        private void Chart2_MouseWheel(object sender, MouseEventArgs e)
        {
            double x_new_size = 0;//X轴的新ScaleView.Size值
            double y_new_size = 0;//Y轴的新ScaleView.Size值
            if (SetAxisLimitFlag2)
            {
                if (e.Delta > 0)//滚轮向上放大
                {
                    if (chart2.ChartAreas[0].AxisX.ScaleView.Size > chart2.ChartAreas[0].AxisX.Minimum)//设置最大放大值
                    {
                        x_new_size = chart2.ChartAreas[0].AxisX.ScaleView.Size - xExtendScale2;//放大
                    }
                    else
                    {
                        //x_new_size = chart2.ChartAreas[0].AxisX.Minimum;
                    }

                    if (chart2.ChartAreas[0].AxisY.ScaleView.Size > chart2.ChartAreas[0].AxisY.Minimum)//设置最大放大值
                    {
                        y_new_size = chart2.ChartAreas[0].AxisY.ScaleView.Size - yExtendScale2;
                    }
                    else
                    {
                        //y_new_size = chart2.ChartAreas[0].AxisY.Minimum;
                    }

                }
                else//滚轮向下缩小
                {
                    if (chart2.ChartAreas[0].AxisX.ScaleView.Size <= (chart2.ChartAreas[0].AxisX.Maximum - chart2.ChartAreas[0].AxisX.Minimum))//不能无限缩小，缩小的极限就是显示全图
                    {
                        x_new_size = chart2.ChartAreas[0].AxisX.ScaleView.Size + xExtendScale2;
                        if (x_new_size >= (chart2.ChartAreas[0].AxisX.Maximum - chart2.ChartAreas[0].AxisX.Minimum))//最大的Size就是整个Y轴
                        {
                            x_new_size = (chart2.ChartAreas[0].AxisX.Maximum - chart2.ChartAreas[0].AxisX.Minimum);
                        }
                    }
                    if (chart2.ChartAreas[0].AxisY.ScaleView.Size <= (chart2.ChartAreas[0].AxisY.Maximum - chart2.ChartAreas[0].AxisY.Minimum))//不能无限缩小，缩小的极限就是显示全图
                    {
                        y_new_size = chart2.ChartAreas[0].AxisY.ScaleView.Size + yExtendScale2;
                        if (y_new_size >= (chart2.ChartAreas[0].AxisY.Maximum - chart2.ChartAreas[0].AxisY.Minimum))//最大的Size就是整个Y轴
                        {
                            y_new_size = (chart2.ChartAreas[0].AxisY.Maximum - chart2.ChartAreas[0].AxisY.Minimum);
                        }
                    }

                }

                if (x_new_size > 0)
                {
                    chart2.ChartAreas[0].AxisX.ScaleView.Size = Math.Round(x_new_size, 2);
                }
                if (y_new_size > 0)
                {
                    chart2.ChartAreas[0].AxisY.ScaleView.Size = Math.Round(y_new_size, 2);

                }

            }
        }

        private void Chart2_MouseLeave(object sender, EventArgs e)
        {
            isMouseDown2 = false;
            chart2.Cursor = Cursors.Default;
            if (chart2.Focused)
            {
                chart2.Parent.Focus();
            }
            toolTip.Hide(this);
        }

        private void Chart2_MouseEnter(object sender, EventArgs e)
        {
            if (!chart2.Focused)
                chart2.Focus();

            toolTip.Show("单击曲线后可拖动,拉伸曲线;双击回到初始位置", chart2, new Point(chart2.Width / 2, chart2.Height / 2), 3000);
        }
        #endregion

        #region 拖动曲线
        private void Chart2_MouseUp(object sender, MouseEventArgs e)
        {
            isMouseDown2 = false;
            chart2.Cursor = Cursors.Default;
        }

        private void Chart2_MouseDown(object sender, MouseEventArgs e)
        {
            isMouseDown2 = true;
            chart2.Cursor = Cursors.SizeAll;
        }

        private void Chart2_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMouseDown2)
            {
                double xvalue = chart2.ChartAreas[0].AxisX.ScaleView.Position;
                double yvalue = chart2.ChartAreas[0].AxisY.ScaleView.Position;
                // 可更改（交换）如下加减1或if条件来设置鼠标移动时曲线移动方向
                if (lastXMove2 != 0 && e.X - lastXMove2 > 0)
                    xvalue -= xShiftScale2;  // 每次移动0.05
                else if (lastXMove2 != 0 && e.X - lastXMove2 < 0)
                    xvalue += xShiftScale2;
                chart2.ChartAreas[0].AxisX.ScaleView.Position = Math.Round(xvalue, 4);
                lastXMove2 = e.X;


                if (lastYMove2 != 0 && lastYMove2 - e.Y > 0)
                    yvalue -= yShiftScale2;
                else if (lastYMove2 != 0 && lastYMove2 - e.Y < 0)
                    yvalue += yShiftScale2;
                chart2.ChartAreas[0].AxisY.ScaleView.Position = Math.Round(yvalue, 4);
                lastYMove2 = e.Y;

            }
        }
        #endregion

        private void Chart2_Click(object sender, EventArgs e)
        {
            try
            {
                if (!SetAxisLimitFlag2)
                {
                    SetAxisLimitFlag2 = true;
                    xMinNum2 = chart2.ChartAreas[0].AxisX.Minimum;
                    xMaxNum2 = chart2.ChartAreas[0].AxisX.Maximum;

                    yMinNum2 = chart2.ChartAreas[0].AxisY.Minimum;
                    yMaxNum2 = chart2.ChartAreas[0].AxisY.Maximum;


                    chart2.ChartAreas[0].AxisX.ScaleView.Size = (xMaxNum2 - xMinNum2);
                    chart2.ChartAreas[0].AxisY.ScaleView.Size = (yMaxNum2 - yMinNum2);

                    xExtendScale2 = (xMaxNum2 - xMinNum2) / 10;
                    xShiftScale2 = xExtendScale2 / 20;

                    yExtendScale2 = (yMaxNum2 - yMinNum2) / 10;
                    yShiftScale2 = yExtendScale2 / 20;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"通道，chart_ScrewDriver_Click->{ex.Message}");
            }
        }

        private void Chart2_DoubleClick(object sender, EventArgs e)
        {
            chart2.ChartAreas[0].AxisX.ScaleView.Size = (xMaxNum2 - xMinNum2);
            chart2.ChartAreas[0].AxisY.ScaleView.Size = (yMaxNum2 - yMinNum2);

            chart2.ChartAreas[0].AxisX.ScaleView.Position = xMinNum2;
            chart2.ChartAreas[0].AxisY.ScaleView.Position = yMinNum2;

        }


        /// <summary>
        /// 加载Chart数据
        /// </summary>
        public void LoadChartData()
        {
            try
            {
                Xparams.Clear();
                Yparams.Clear();
                Y2params.Clear();
                CurveModel curveModel = CurveDbHelper.GetLastCurve();
                string[] torquesStrList = curveModel.Torques.Split(',');
                for (int i = 0; i < torquesStrList.Length; i++)
                {
                    if (double.TryParse(torquesStrList[i], out double value))
                    {
                        Y2params.Add(value);

                    }
                }
                string[] tanglesStrList = curveModel.Angles.Split(',');
                for (int i = 0; i < tanglesStrList.Length; i++)
                {
                    if (double.TryParse(tanglesStrList[i], out double value))
                    {
                        Yparams.Add(value);
                    }
                }
                for (int i = 0; i < Y2params.Count; i++)
                {
                    Xparams.Add((int)(curveModel.TimeSpan * i * 1000));
                    //Xparams.Add(Math.Round(curveModel.TimeSpan * i, 3));
                }


                SetAxisLimitFlag1 = false;
                if (chart1.Series.Count > 0)
                {
                    chart1.Series.Clear();
                }
                // 第一个坐标轴
                Series seriesN1 = new Series();
                seriesN1.Name = string.Empty;
                seriesN1.LegendText = "";
                seriesN1.IsVisibleInLegend = false;
                seriesN1.BorderWidth = 2;
                seriesN1.Color = seriesColor[0];
                seriesN1.ChartArea = "ChartArea1";
                seriesN1.ChartType = SeriesChartType.Spline;
                for (int i = 0; i < Yparams.Count; i++)
                {
                    double x = Yparams[i];
                    double y = Y2params[i];
                    seriesN1.Points.AddXY(x, y);
                }
                chart1.Series.Add(seriesN1);
                chart1.ChartAreas[0].AxisX.Title = "力矩(Nm)";
                chart1.ChartAreas[0].AxisX.TitleForeColor = Color.Green;
                chart1.ChartAreas[0].AxisX.TitleFont = axisFont;
                chart1.ChartAreas[0].AxisY.Title = "角度(°)";
                chart1.ChartAreas[0].AxisY.TextOrientation = TextOrientation.Rotated90;
                chart1.ChartAreas[0].AxisY.TitleForeColor = seriesColor[0];
                chart1.ChartAreas[0].AxisY.TitleFont = axisFont;


                SetAxisLimitFlag2 = false;
                if (chart2.Series.Count > 0)
                {
                    chart2.Series.Clear();
                }
                Series seriesN2 = new Series();
                seriesN2.Name = string.Empty;
                seriesN2.LegendText = "";
                seriesN2.IsVisibleInLegend = false;
                seriesN2.BorderWidth = 2;
                seriesN2.Color = seriesColor[1];
                seriesN2.ChartArea = "ChartArea1";
                seriesN2.ChartType = SeriesChartType.Spline;
                for (int i = 0; i < Xparams.Count; i++)
                {
                    double x = Xparams[i];
                    double y = Yparams[i];
                    seriesN2.Points.AddXY(x, y);
                }
                chart2.Series.Add(seriesN2);
                chart2.ChartAreas[0].AxisX.Title = "时间(ms)";
                chart2.ChartAreas[0].AxisX.TitleForeColor = Color.Green;
                chart2.ChartAreas[0].AxisX.TitleFont = axisFont;
                chart2.ChartAreas[0].AxisY.Title = "力矩(Nm)";
                chart2.ChartAreas[0].AxisY.TextOrientation = TextOrientation.Rotated90;
                chart2.ChartAreas[0].AxisY.TitleForeColor = seriesColor[1];
                chart2.ChartAreas[0].AxisY.TitleFont = axisFont;

                chart2.ChartAreas[0].AxisX.ScaleView.Position = 0;

            }
            catch (Exception ex)
            {
                MessageBox.Show($"通道，LoadChartData->{ex.Message}");
            }

        }

        ///// <summary>
        ///// 显示曲线
        ///// </summary>
        //public void LoadDDKChartData()
        //{
        //    try
        //    {
        //        torqueAngleList.Clear();
        //        curveAngleList.Clear();
        //        torqueTimeList.Clear();
        //        curveTimeList.Clear();

        //        DDKCurveModel curveModel = CurveDbHelper.GetDDKCurveByTaskID(CurrentTaskId);
        //        if (curveModel == null)
        //        {
        //            return;
        //        }
        //        curveBaseDeg = curveModel.AngleStep;
        //        string[] torqueanglesList = curveModel.AngleTorques.Split(',');
        //        for (int i = 0; i < torqueanglesList.Length; i++)
        //        {
        //            if (double.TryParse(torqueanglesList[i], out double value))
        //            {
        //                torqueAngleList.Add(value);
        //                curveAngleList.Add(curveBaseDeg + curveBaseDeg * i);
        //            }
        //        }
        //        string[] torquetimesList = curveModel.TimeTorques.Split(',');
        //        curveBaseTime = curveModel.TimeStep;
        //        for (int i = 0; i < torquetimesList.Length; i++)
        //        {
        //            if (double.TryParse(torquetimesList[i], out double value))
        //            {
        //                torqueTimeList.Add(value);
        //                curveTimeList.Add(curveBaseTime + curveBaseTime * i);
        //            }
        //        }

        //        SetAxisLimitFlag1 = false;
        //        if (chart1.Series.Count > 0)
        //        {
        //            chart1.Series.Clear();
        //        }
        //        // 第一个坐标轴
        //        Series seriesN1 = new Series();
        //        seriesN1.Name = string.Empty;
        //        seriesN1.LegendText = "";
        //        seriesN1.IsVisibleInLegend = false;
        //        seriesN1.BorderWidth = 2;
        //        seriesN1.Color = seriesColor[0];
        //        seriesN1.ChartArea = "ChartArea1";
        //        seriesN1.ChartType = SeriesChartType.Spline;
        //        for (int i = 0; i < torqueAngleList.Count; i++)
        //        {
        //            double x = curveAngleList[i];
        //            double y = torqueAngleList[i];
        //            seriesN1.Points.AddXY(x, y);
        //        }
        //        chart1.Series.Add(seriesN1);
        //        chart1.ChartAreas[0].AxisX.Title = "角度(°)";
        //        chart1.ChartAreas[0].AxisX.TitleForeColor = Color.Green;
        //        chart1.ChartAreas[0].AxisX.TitleFont = axisFont;
        //        chart1.ChartAreas[0].AxisY.Title = "力矩(Nm)";
        //        chart1.ChartAreas[0].AxisY.TextOrientation = TextOrientation.Rotated90;
        //        chart1.ChartAreas[0].AxisY.TitleForeColor = seriesColor[0];
        //        chart1.ChartAreas[0].AxisY.TitleFont = axisFont;



        //        SetAxisLimitFlag2 = false;
        //        if (chart2.Series.Count > 0)
        //        {
        //            chart2.Series.Clear();
        //        }
        //        Series seriesN2 = new Series();
        //        seriesN2.Name = string.Empty;
        //        seriesN2.LegendText = "";
        //        seriesN2.IsVisibleInLegend = false;
        //        seriesN2.BorderWidth = 2;
        //        seriesN2.Color = seriesColor[1];
        //        seriesN2.ChartArea = "ChartArea1";
        //        seriesN2.ChartType = SeriesChartType.Spline;
        //        for (int i = 0; i < torqueTimeList.Count; i++)
        //        {
        //            double x = curveTimeList[i];
        //            double y = torqueTimeList[i];
        //            seriesN2.Points.AddXY(x, y);
        //        }
        //        chart2.Series.Add(seriesN2);
        //        chart2.ChartAreas[0].AxisX.Title = "时间(ms)";
        //        chart2.ChartAreas[0].AxisX.TitleForeColor = Color.Green;
        //        chart2.ChartAreas[0].AxisX.TitleFont = axisFont;
        //        chart2.ChartAreas[0].AxisY.Title = "力矩(Nm)";
        //        chart2.ChartAreas[0].AxisY.TextOrientation = TextOrientation.Rotated90;
        //        chart2.ChartAreas[0].AxisY.TitleForeColor = seriesColor[1];
        //        chart2.ChartAreas[0].AxisY.TitleFont = axisFont;

        //        chart2.ChartAreas[0].AxisX.ScaleView.Position = 0;

        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show($"通道，LoadChartData->{ex.Message}");
        //    }
        //}

        /// <summary>
        /// 显示曲线
        /// </summary>
        public void LoadDDKChartData()
        {
            try
            {
                torqueAngleList.Clear();
                curveAngleList.Clear();
                torqueTimeList.Clear();
                curveTimeList.Clear();

                //mod by ming 11-25
                lbl_Curve.Text = $"条码【{_productSN}】拧紧曲线";
                var curveList = CurveDbHelper.GetDDKCurveByTaskID(CurrentTaskId, _productSN);
                if (curveList != null && curveList.Count > 0)
                {
                    #region 角度和时间曲线 mod by ming 11-25   
                    //1. 角度  
                    curveBaseDeg = curveList[0].AngleStep;
                    string[] torqueanglesList = curveList[0].AngleTorques.Split(',');
                    for (int i = 0; i < torqueanglesList.Length; i++)
                    {
                        if (double.TryParse(torqueanglesList[i], out double value))
                        {
                            torqueAngleList.Add(value);
                            curveAngleList.Add(curveBaseDeg + curveBaseDeg * i);
                        }
                    }

                    //2. 时间
                    string[] torquetimesList = curveList[1].TimeTorques.Split(',');
                    //mod by ming 11 - 25
                    curveBaseTime = curveList[1].TimeStep;
                    for (int i = 0; i < torquetimesList.Length; i++)
                    {
                        if (double.TryParse(torquetimesList[i], out double value))
                        {
                            torqueTimeList.Add(value);
                            curveTimeList.Add(curveBaseTime + curveBaseTime * i);
                        }
                    }

                    #endregion mod by ming 11-25 

                    SetAxisLimitFlag1 = false;
                    if (chart1.Series.Count > 0)
                    {
                        chart1.Series.Clear();
                    }
                    // 第一个坐标轴
                    Series seriesN1 = new Series
                    {
                        Name = string.Empty,
                        LegendText = "",
                        IsVisibleInLegend = false,
                        BorderWidth = 2,
                        Color = seriesColor[0],
                        ChartArea = "ChartArea1",
                        ChartType = SeriesChartType.Spline
                    };
                    for (int i = 0; i < torqueAngleList.Count; i++)
                    {
                        double x = curveAngleList[i];
                        double y = torqueAngleList[i];
                        seriesN1.Points.AddXY(x, y);
                    }
                    chart1.Series.Add(seriesN1);
                    chart1.ChartAreas[0].AxisX.Title = "角度(°)";
                    chart1.ChartAreas[0].AxisX.TitleForeColor = Color.Green;
                    chart1.ChartAreas[0].AxisX.TitleFont = axisFont;
                    chart1.ChartAreas[0].AxisY.Title = "力矩(Nm)";
                    chart1.ChartAreas[0].AxisY.TextOrientation = TextOrientation.Rotated90;
                    chart1.ChartAreas[0].AxisY.TitleForeColor = seriesColor[0];
                    chart1.ChartAreas[0].AxisY.TitleFont = axisFont;

                    SetAxisLimitFlag2 = false;
                    if (chart2.Series.Count > 0)
                    {
                        chart2.Series.Clear();
                    }
                    Series seriesN2 = new Series
                    {
                        Name = string.Empty,
                        LegendText = "",
                        IsVisibleInLegend = false,
                        BorderWidth = 2,
                        Color = seriesColor[1],
                        ChartArea = "ChartArea1",
                        ChartType = SeriesChartType.Spline
                    };
                    for (int i = 0; i < torqueTimeList.Count; i++)
                    {
                        double x = curveTimeList[i];
                        double y = torqueTimeList[i];
                        seriesN2.Points.AddXY(x, y);
                    }
                    chart2.Series.Add(seriesN2);
                    chart2.ChartAreas[0].AxisX.Title = "时间(ms)";
                    chart2.ChartAreas[0].AxisX.TitleForeColor = Color.Green;
                    chart2.ChartAreas[0].AxisX.TitleFont = axisFont;
                    chart2.ChartAreas[0].AxisY.Title = "力矩(Nm)";
                    chart2.ChartAreas[0].AxisY.TextOrientation = TextOrientation.Rotated90;
                    chart2.ChartAreas[0].AxisY.TitleForeColor = seriesColor[1];
                    chart2.ChartAreas[0].AxisY.TitleFont = axisFont;
                    chart2.ChartAreas[0].AxisX.ScaleView.Position = 0;
                }
                else
                {
                    chart1.Series.Clear();
                    chart2.Series.Clear();
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show($"通道，LoadDDKChartData->{ex.Message}");
                UnifiedObj.WriteLog(1, $"通道，LoadDDKChartData->{ex.Message}");
            }
        }
    }
}
